package com.struts.deal.admin.station;


import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import com.base.Constants;
import com.ibatis.po.system.TSysAdmin;
import com.ibatis.po.user.TStation;
import com.ibatis.po.user.TSubStation;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CSqlMap;
import com.lg.system.CTurnPage;
import com.lg.system.Common;
import com.lg.system.MessageBean;
import com.struts.web.manage.admin.popedom.UserManageForm;
import com.struts.web.manage.admin.station.AddKindOfStaForm;
import com.struts.web.manage.admin.station.AddSubStaForm;

public class Deal {

	/**
	 * 工作岗位类别浏览
	 * @param request
	 * @param aWebForm
	 */
	public static void viewStationsList(HttpServletRequest request ){
		CTurnPage page = new CTurnPage();
		
		try{
			page.setQueryList("T_STATION.selectAll");
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 修改岗位类别信息加载
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean loadEditStationsList(HttpServletRequest request,AddKindOfStaForm aWebForm ){
		CTurnPage page = new CTurnPage();
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TStation staPo = new TStation();
		String sortId = request.getParameter("staId");
		try{
			staPo.setId(sortId);
			staPo = (TStation)sqlmap.queryForObject("T_STATION.abatorgenerated_selectByPrimaryKey",staPo);
			aWebForm.setSortName(staPo.getSortName());
			aWebForm.setStatus(staPo.getStatus());
			aWebForm.setStaId(sortId);
			
			page.setQueryList("T_SUB_STATION.selectBySortId",sortId);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
			
			messageBean.setResult(true);
		}catch(Exception e){
			messageBean.setMessage(Constants.LOAD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	/**
	 * 修改岗位信息加载
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean loadEditSubSta(HttpServletRequest request,AddSubStaForm aWebForm ){
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSubStation subStaPo = new TSubStation();
		String subStaId = request.getParameter("subStaId");
		try{
			subStaPo.setId(subStaId);
			subStaPo = (TSubStation)sqlmap.queryForObject("T_SUB_STATION.selectByPriKey",subStaPo);
			aWebForm.setPostName(subStaPo.getPostName());
			aWebForm.setStatus(subStaPo.getStatus());
			aWebForm.setSubStaId(subStaPo.getId());
			
			messageBean.setResult(true);
		}catch(Exception e){
			messageBean.setMessage(Constants.LOAD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	/**
	 * 添加岗位类别
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean addKindOfSta(HttpServletRequest request,AddKindOfStaForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TStation staPo = new TStation();
		int count = 0;
		String sortName = aWebForm.getSortName();
		try{

			if(sortName!=null && sortName!=""){
				count = ((Integer)sqlmap.queryForObject("T_STATION.selectIsExist",sortName)).intValue();
				if(count<1){
					staPo.setId(Common.getNewID());
					staPo.setSortName(aWebForm.getSortName());
					staPo.setStatus("1");
					sqlmap.insert("T_STATION.abatorgenerated_insert",staPo);
					messageBean.setResult(true);
				}
			}else {
			messageBean.setMessage(Constants.ADD_FAIL);
			}

		}catch(Exception e){
			messageBean.setMessage(Constants.ADD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	
	/**
	 * 添加岗位
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean addSubSta(HttpServletRequest request,AddSubStaForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSubStation subStaPo = new TSubStation();
		int count = 0;
		String subStaName = aWebForm.getPostName();
		try{

			if(subStaName!=null && subStaName!=""){
				count = ((Integer)sqlmap.queryForObject("T_SUB_STATION.selectIsExist",subStaName)).intValue();
				if(count<1){
					subStaPo.setId(Common.getNewID());
					subStaPo.setPostName(subStaName);
					subStaPo.setSortId(aWebForm.getStaId());
					System.out.println("ssssssssssssssss:"+aWebForm.getStaId());
					subStaPo.setStatus("1");
					sqlmap.insert("T_SUB_STATION.insertBySortId",subStaPo);
					messageBean.setResult(true);
				}
			}else {
			messageBean.setMessage(Constants.ADD_FAIL);
			}

		}catch(Exception e){
			messageBean.setMessage(Constants.ADD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	
	/**
	 * 修改岗位类别信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean editStationsList(HttpServletRequest request,AddKindOfStaForm aWebForm ) throws SQLException{
		
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TStation staPo = new TStation();
		String staId = aWebForm.getStaId();
		try{
			staPo.setId(staId);
			staPo.setSortName(aWebForm.getSortName());
			staPo.setStatus(aWebForm.getStatus());
			int count = sqlmap.update("T_STATION.abatorgenerated_updateByPrimaryKey",staPo);
			
			if(count>0){
				
				messageBean.setResult(true);
			}else{
				messageBean.setMessage(Constants.EDIT_FAIL);
			}
			
		}catch(Exception e){
			messageBean.setMessage(Constants.EDIT_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	
	/**
	 * 修改岗位信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean editSubSta(HttpServletRequest request,AddSubStaForm aWebForm ) throws SQLException{
		
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSubStation subStaPo = new TSubStation();
		try{
			subStaPo.setPostName(aWebForm.getPostName());
			subStaPo.setStatus(aWebForm.getStatus());
			subStaPo.setId(aWebForm.getSubStaId());
			int count = sqlmap.update("T_SUB_STATION.updateByPriKey",subStaPo);
			if(count>0){
				
				messageBean.setResult(true);
			}else{
				messageBean.setMessage(Constants.EDIT_FAIL);
			}
			
		}catch(Exception e){
			messageBean.setMessage(Constants.EDIT_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	
	/**
	 * 删除岗位类别信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean delStationsList(HttpServletRequest request,AddKindOfStaForm aWebForm ) throws SQLException{
		
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TStation staPo = new TStation();
		String staId = aWebForm.getStaId();
		TSubStation subStaPo = new TSubStation();
		int count=0;
		try{
			staPo.setId(staId);
			subStaPo.setSortId(staId);
			count = sqlmap.delete("T_STATION.abatorgenerated_deleteByPrimaryKey",staPo);
			System.out.println("Count:"+count);
			if(count>0){
				count = sqlmap.delete("T_SUB_STATION.deleteBySortId",subStaPo);
				System.out.println("Count1:"+count);
			}
				messageBean.setResult(true);
		}catch(Exception e){
			messageBean.setMessage(Constants.DEL_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	/**
	 * 删除岗位信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean delSubSta(HttpServletRequest request,AddSubStaForm aWebForm ) throws SQLException{
		
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSubStation subStaPo = new TSubStation();
		
		int count=0;
		try{
			subStaPo.setId(aWebForm.getSubStaId());
			count = sqlmap.delete("T_SUB_STATION.deleteById",subStaPo);
			if(count>0){
				messageBean.setResult(true);
				System.out.println("Count1:"+count);
			}else{
				messageBean.setMessage(Constants.DEL_FAIL);
			}
			
			
			
				
		}catch(Exception e){
			messageBean.setMessage(Constants.DEL_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
}
